import { defineStore } from 'pinia'
import { getBannerList, getGridList,getSeckilllist,getGoodsList } from '@/api/home'
export const useHomeStore = defineStore('useHomeStore', {
  state() {
    return {
      // bannerList: localStorage.getItem("bannerList") || [],//轮播图
      // gridList: localStorage.getItem("gridList") || [],//宫格数据
      // seckilllist:localStorage.getItem("seckilllist") || [],//秒杀列表
      // goodsList:localStorage.getItem("goodsList") || [],//商品列表
      bannerList:[],//轮播图
      gridList:[],//宫格数据
      seckilllist:[],//秒杀列表
      goodsList:[],//商品列表
    }
  },
  actions: {
    //TODO 发送请求,获取轮播图图片的数据
    async getBannerListHandler() {
      //发送请求
      const result = await getBannerList();
      if ((result as any).code == 200) {
        //将数据交给pinia
        this.bannerList = result.data
        //数据持久化
        // localStorage.setItem("bannerList", result.data)
      }
    },
    //TODO 发送请求,获取宫格的数据
    async getGridListHandler() {
      //发送请求
      const result = await getGridList();
      (this as any).gridList = result
      //数据持久化
      // localStorage.setItem("gridList",(result as any))
    },
    //TODO 发送请求,秒杀列表数据
    async getSeckilllistHandler(data:{count:number,limitNum:number}) {
      //发送请求
      const result = await getSeckilllist(data);
      if ((result as any).code == 200) {
        //将数据交给pinia
        this.seckilllist = result.data
        //数据持久化
        // localStorage.setItem("seckilllist", result.data)
      }
    },
    //TODO 发送请求,商品列表信息的数据
    async getGoodsListHandler(data:{count:number,limitNum:number}) {
      //发送请求
      const result = await getGoodsList(data);
      if ((result as any).code == 200) {
        //判断,如果获取到的数据是一个空的,那么我们就不需要再次获取数据了
        if(result.data.length == 0){
            return Promise.reject("不要获取了")
        }
        //将数据交给pinia
        //concat 合并数组
        this.goodsList = this.goodsList.concat(result.data)
        //数据持久化
        // localStorage.setItem("goodsList",JSON.stringify(this.goodsList))
      }
    },
  }
})
